<?php
/**
 * Browser for tables containing more rows than defined in config file.
 * Parameters from page including this one:
 * 		$headline: the headline for the page
 * 		$count: the number of rows to be displayed
 * 		$type_of_list: the name for the item being displayded (e.g. "Calls")
 * 		$width: the width of the table
 * Returns following paramters to including page:
 * 		$displayFrom: the number from which on the items are showed (e.g. from '5')
 * 		$displayTo: the number to which the items are schow (e.g. to '10')
 */
$DISPLAY_ROWS = 60;

//Sum of rows to display
if (!isset($count) || !is_numeric($count) || !isset($type_of_list) || !isset($width) || !isset($headline)) die("Error browsing data!");

//Indicating page to display, initially: 1.
$page=1;

//Offset is added if buttons forward or back have been pushed, initially: 0.
$offSet=0;

//If buttons were pushed before now, offset has been set in hidden
//field and must be updated from there.
if (isset($_POST["offSet"])) $offSet=$_POST["offSet"];

//The count of pages available. Rounded to the next highest integer value.
$pages=ceil($count/$DISPLAY_ROWS);

//Forward button pushed
if (isset($_POST["goForward"])){
	//Increase offset.
	$offSet++;
}

//Back button pushed, decrease offset analogues to forward button.
if (isset($_POST["goBack"])){
	$offSet--;
}

//Add offset to page number to be displayed.
$page+=$offSet;

//If page is bigger than available pages, restrict to maximum page and offset.
if ($page>$pages){
	$page=$pages;
	$offSet=$pages-1;
}

//If page is smaller than page 1, switch to page 1 and set offset to 0.
if ($page<1){
	$page=1;
	$offSet=0;
}

//Save current offset in hidden field.
echo "<input type=hidden name=offSet value=$offSet>";
//Displaying from call number $displayFrom.
$displayFrom=$page*$DISPLAY_ROWS-$DISPLAY_ROWS+1;
if ($count==0) $displayFrom=0;

//Diplaying to call number $displayTo.
$displayTo=$page*$DISPLAY_ROWS;
if ($displayTo>$count) $displayTo=$count;

$buttonShowAll="<input type='submit' name='browser_show_all' value='Alle' class='small_button'>";
if (isset($_POST["browser_show_all"]) || $pages==1){
	$pages=1;
	$displayFrom=1;
	$displayTo=$count;
	$buttonShowAll="";
}

echo "
<table width='$width' class='browser'>
<tr>
	<td align='left'>
		$headline
	</td>
	<td align='right'>
		<table class='browser'>
	";

	//Show back and forward buttons if more than one page
	//is needed for displaying.
	if ($pages>1) {
		$buttonL="<input type='submit' class='submit_button_RL' title='back' value='' name='goBack' onMouseOver='swapBrowerButtonL(this)' onMouseOut='swapBrowerButtonL(this)'>";
		$buttonR="<input type='submit' class='submit_button_LR' title='forward' value='' name='goForward' onMouseOver='swapBrowerButtonR(this)' onMouseOut='swapBrowerButtonR(this)'>";
		if ($page==1) $buttonL="&nbsp;";//On first page, don't show back button.
		if ($page==$pages) $buttonR="&nbsp;";//On last page, don't show forward button.		
		echo "
			<tr>
				<td style='width: 16px'>$buttonL</td>
				<td style='text-align:center'>$buttonShowAll</td>
				<td style='width: 16px'>$buttonR</td>
			<tr>			
		";
	}
	echo "
			<tr>
				<td colspan='3' style='text-align:right'>$type_of_list $displayFrom bis $displayTo von $count</td>		
			</tr>
		</table>
	</td>
</tr>
<tr>
	<td colspan='2'><hr></td>
</tr>
</table>\n";

#Following JS script is needed only for IE, since :hover statement only supported for <a>-tags.
?>

<script type="text/javascript">
	
	function swapBrowerButtonR(object)
	{

		if (object.className=='submit_button_LR')
		{
			object.className='submit_button_LR_hover';
		    return;
		}
		if (object.className=='submit_button_LR_hover') 
			object.className='submit_button_LR';
	}
	
	function swapBrowerButtonL(object)
	{

		if (object.className=='submit_button_RL')
		{
			object.className='submit_button_RL_hover';
		    return;
		}
		if (object.className=='submit_button_RL_hover') 
			object.className='submit_button_RL';
	}	
</script>